{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "89355c86-070f-4955-95f7-c6843566fffe",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# $L^{\\infty}$ 范数\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d0cb62d-e7f4-49a3-a28e-197b2f60934b",
   "metadata": {},
   "source": [
    "这段代码的主要目的是计算给定向量 $a$ 和 $b$ 的 $L^{\\infty}$ 范数（最大范数，或称为切比雪夫范数）。我们从数学角度来详细解析其背后的概念和计算过程。\n",
    "\n",
    "---\n",
    "\n",
    "### 1. $L^p$ 范数的定义\n",
    "\n",
    "在数学中，$L^p$ 范数（Minkowski 范数）用于衡量向量的大小，它的定义如下：\n",
    "\n",
    "$$\n",
    "\\| x \\|_p = \\left( \\sum_{i=1}^{n} |x_i|^p \\right)^{\\frac{1}{p}}\n",
    "$$\n",
    "\n",
    "当 $p = \\infty$ 时，$L^\\infty$ 范数定义为：\n",
    "\n",
    "$$\n",
    "\\| x \\|_{\\infty} = \\max_{i} |x_i|\n",
    "$$\n",
    "\n",
    "即向量中所有元素的绝对值的最大值。\n",
    "\n",
    "---\n",
    "\n",
    "### 2. 代码解析\n",
    "\n",
    "#### **2.1 初始化向量**\n",
    "```python\n",
    "a = np.array([3, -4])\n",
    "b = np.array([0, 5])\n",
    "```\n",
    "这里定义了两个向量：\n",
    "- 向量 $a = (3, -4)$\n",
    "- 向量 $b = (0, 5)$\n",
    "\n",
    "#### **2.2 计算 $L^{\\infty}$ 范数**\n",
    "```python\n",
    "L_inf_a = np.linalg.norm(a, ord=np.inf)\n",
    "```\n",
    "这一步计算向量 $a$ 的 $L^{\\infty}$ 范数。根据定义：\n",
    "$$\n",
    "\\| a \\|_{\\infty} = \\max(|3|, |-4|) = 4\n",
    "$$\n",
    "\n",
    "同理：\n",
    "```python\n",
    "L_inf_b = np.linalg.norm(b, ord=np.inf)\n",
    "```\n",
    "计算向量 $b$ 的 $L^{\\infty}$ 范数：\n",
    "$$\n",
    "\\| b \\|_{\\infty} = \\max(|0|, |5|) = 5\n",
    "$$\n",
    "\n",
    "#### **2.3 直接用 `np.max(np.abs(...))` 验证**\n",
    "```python\n",
    "np.max(np.abs(a))\n",
    "np.max(np.abs(b))\n",
    "```\n",
    "`np.abs(a)` 计算向量中每个元素的绝对值：\n",
    "$$\n",
    "\\text{abs}(a) = (3, 4)\n",
    "$$\n",
    "然后 `np.max(...)` 取最大值：\n",
    "$$\n",
    "\\max(3, 4) = 4\n",
    "$$\n",
    "与 `np.linalg.norm(a, ord=np.inf)` 结果一致。\n",
    "\n",
    "同样：\n",
    "$$\n",
    "\\text{abs}(b) = (0, 5)\n",
    "$$\n",
    "$$\n",
    "\\max(0, 5) = 5\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### 3. 代码整体逻辑\n",
    "1. **初始化向量 $a, b$**\n",
    "2. **使用 `np.linalg.norm(..., ord=np.inf)` 计算 $L^{\\infty}$ 范数**\n",
    "3. **用 `np.max(np.abs(...))` 验证计算结果**\n",
    "\n",
    "最终结果：\n",
    "$$\n",
    "\\| a \\|_{\\infty} = 4, \\quad \\| b \\|_{\\infty} = 5\n",
    "$$\n",
    "\n",
    "这个范数在优化、信号处理和矩阵分析中常用于衡量向量的最大幅值，特别适用于考虑最坏情况的分析。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "047869d8-ba1c-4683-8339-f9d21790b89e",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2dac3853-6f03-42cc-9ce8-9dc7b366b49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66762b2b-cff2-4d5a-aac7-242bfb930147",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f42cddaa-69ba-40e5-9828-6a18c6715101",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([3, -4])\n",
    "b = np.array([0, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94ef4522-2867-406e-92a6-ea6cd4d14b46",
   "metadata": {},
   "source": [
    "## 计算 $L^{\\infty}$ 范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "255aa946-b983-4673-ac7e-fb1244debbf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L_inf_a = np.linalg.norm(a, ord=np.inf) # 默认\n",
    "L2_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c60db7d9-96f8-4dd6-bf58-09f64eba698c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(np.abs(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8ae91f0d-9d50-4af8-93de-bf9bd55ee2ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "L_inf_b = np.linalg.norm(b, ord=np.inf) # 默认"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dbf08311-7b62-4ef4-81c2-93ab8d95c9c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(np.abs(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "648725c8-da6a-4247-a0ea-364758ac52bf",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
